package lwh.exmpackage;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwh.utils.DBHelper;
import lwh.utils.DateUtil;

public class EPCommitMessage extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String prj_exm_name=new String(request.getParameter("prj_exm_name").getBytes("ISO8859-1"),"UTF-8");
		String prj_exm_desc=new String(request.getParameter("prj_exm_desc").getBytes("ISO8859-1"),"UTF-8");
		String exm_pck_id=new String(request.getParameter("exm_pck_id").getBytes("ISO8859-1"),"UTF-8");
		String prj_exm_type=new String(request.getParameter("prj_exm_type").getBytes("ISO8859-1"),"UTF-8");
		String prj_unit_batch_id=new String(request.getParameter("prj_unit_batch_id").getBytes("ISO8859-1"),"UTF-8");
		String sql="insert into prj_exm_main(prj_unit_batch_id,exm_pck_id,prj_exm_name,prj_exm_desc,prj_exm_type,prj_exm_status,prj_exm_id) values (?,?,?,?,?,?,?)";
		Connection con=null;
		boolean b=false;;
		PreparedStatement stat=null;
		try {
			con=DBHelper.getConnection();
			stat=con.prepareStatement(sql);
			stat.setString(1, prj_unit_batch_id);
			stat.setString(2, exm_pck_id);
			stat.setString(3, prj_exm_name);
			stat.setString(4, prj_exm_desc);
			stat.setString(5, prj_exm_type);
			stat.setString(6, "1");
			stat.setString(7, UUID.randomUUID().toString());
			b=stat.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				con.close();
				stat.close();
				if(b){
					response.getWriter().write(1);
				}else{
					response.getWriter().write(0);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
